package com.net.netmonitor.util;

import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.Properties;

/**
 * 获取配置文件的值
 */
public class PropertiesFileUtil {

    private static String Root = "";

    //当前项目jar包所在目录
    public static String getRootPath(){
        try{
            if(StringUtils.isEmpty(Root)){
                File path = new File(ResourceUtils.getURL("classpath:").getPath());
                if(!path.exists()) path = new File("");
                //当前项目jar包所在目录
                Root = path.getAbsolutePath();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Root;
    }

    public static String getValueByKey(String key, String filePath) {
        Properties pps = new Properties();
        try {
            InputStream in = new BufferedInputStream (new FileInputStream(filePath));
            pps.load(in);
            String value = pps.getProperty(key);
//            System.out.println(key + " = " + value);
            return value;

        }catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
